package org.ztr.yanai.ai.service.impl;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.stereotype.Service;
import org.ztr.yanai.ai.service.PromptTemplateService;
import reactor.core.publisher.Flux;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;
import java.util.Map;


/**
 * @Author: ZhaoTR
 * @Date: Created in 2025/11/1 16:10
 * @Description: 测试 PromptTemplate
 * @Version: 1.0
 */

@Service
public class PromptTemplateServiceImpl implements PromptTemplateService {

    @Resource(name = "deepseek")
    private ChatModel deepseekChatModel;

    @Resource(name = "deepseekChatClient")
    private ChatClient deepseekChatClient;

    @Value("classpath:/prompttemplate/atguigu-template.txt")
    private org.springframework.core.io.Resource userTemplate;

    @Override
    public Flux<String> chat(String topic, String outputFormat, String wordCount) {
        PromptTemplate promptTemplate = new PromptTemplate(
                "讲一个关于{topic}的故事并以{output_format}格式输出，字数在{wordCount}左右"
        );
        Prompt prompt = promptTemplate.create(Map.of(
                "topic", topic,
                "output_format", outputFormat,
                "wordCount", wordCount
        ));
        return deepseekChatClient.prompt(prompt).stream().content();
    }

    @Override
    public String chat2(String topic, String outputFormat) {
        PromptTemplate promptTemplate = new PromptTemplate(userTemplate);
        Prompt prompt = promptTemplate.create(Map.of("topic", topic, "output_format", outputFormat));
        return deepseekChatClient.prompt(prompt).call().content();
    }

    @Override
    public String chat3(String sysTopic, String userTopic) {
        SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(
                "你是{systemTopic}助手，只回答{systemTopic}其它无可奉告，以HTML格式的结果。"
        );
        Message sysMessage = systemPromptTemplate.createMessage(Map.of("systemTopic", sysTopic));

        PromptTemplate userPromptTemplate = new PromptTemplate("解释一下{userTopic}");
        Message userMessage = userPromptTemplate.createMessage(Map.of("userTopic", userTopic));

        Prompt prompt = new Prompt(List.of(sysMessage, userMessage));
        return deepseekChatClient.prompt(prompt).call().content();
    }

    @Override
    public String chat4(String question) {
        SystemMessage systemMessage = new SystemMessage("你是一个Java编程助手，拒绝回答非技术问题。");
        UserMessage userMessage = new UserMessage(question);
        Prompt prompt = new Prompt(List.of(systemMessage, userMessage));
        return deepseekChatModel.call(prompt).getResult().getOutput().getText();
    }

    @Override
    public Flux<String> chat5(String question) {
        return deepseekChatClient.prompt()
                .system("你是一个Java编程助手，拒绝回答非技术问题。")
                .user(question)
                .stream()
                .content();
    }
}
